#include "AutoControl1028.h"
#include "TinyXML/tinyxml.h"
#include "ChineseEnglishMapping.h"
#include <QDebug>
#include <QVBoxLayout>
#include <QSplitter>

AutoControl1028::AutoControl1028()
{

}

void AutoControl1028::InitUI(const QString &fileName, QList<QPair<QWidget *, QString> > &widgets, QMap<int, QList<TableWidgetForm *> > &m_pTableWidgetFormMap)
{
    QString filePath = QString("%1/config/%2/%3")
            .arg(COMMONS->XmlPath)
            .arg(COMMONS->MachType)
            .arg(fileName);
    QList<AutoItem> _AutoItemList;
    QList<AutoTemp> _AutoTempList;
    QList<AutoArea> _AutoAreaList;
    QList<DevModeItem> _DevModeItemList;
    QList<QPair<AreasArea, QList<AreaGear>>> _AreasList;
    Analysis(filePath,_AutoItemList,_AutoTempList,_AutoAreaList,_DevModeItemList,_AreasList);

    QWidget* widget = new QWidget;
    QVBoxLayout *layout = new QVBoxLayout(widget);
    widgets.append(qMakePair(widget,QStringLiteral("tab0")));
    TableWidgetForm* _TableWidgetForm = new TableWidgetForm(widget);
    layout->addWidget(_TableWidgetForm);
    _TableWidgetForm->setGroupBoxTitle(QString("%1_0").arg(fileName.left(fileName.size()-4)));

    //获取表头
    AutoItem _AtTimeStructtemp;
    _TableWidgetForm->setTableWidgetHeaderLabels(COMMONS->getTableWidgetHeadLabels(fileName,_AtTimeStructtemp.toFieldNames().split(',')));
    _TableWidgetForm->setTableWidgetHeaderType(_AtTimeStructtemp.toFieldTypes());

    for (int j = 0; j < _AutoItemList.size(); j++)
    {
        _TableWidgetForm->getTableWidget()->setRowCount(j+1);
        _TableWidgetForm->getTableWidget()->setItem(j, 0, new QTableWidgetItem(_AutoItemList.at(j).name));
        _TableWidgetForm->getTableWidget()->item(j, 0)->setData(Qt::UserRole, _AutoItemList.at(j).id);
        _TableWidgetForm->getTableWidget()->setItem(j, 1, new QTableWidgetItem(QString("%1").arg(_AutoItemList.at(j).val)));
        _TableWidgetForm->getTableWidget()->setItem(j, 2, new QTableWidgetItem(QString("%1").arg(_AutoItemList.at(j).desc)));
    }
    QList<TableWidgetForm*> item = m_pTableWidgetFormMap.value(0);
    m_pTableWidgetFormMap.insert(0, item << _TableWidgetForm);

    //添加第一个表
    QWidget* widget2 = new QWidget;
    QVBoxLayout *layout2 = new QVBoxLayout(widget2);
    QSplitter *splitter = new QSplitter(Qt::Vertical, widget2);
    splitter->setChildrenCollapsible(false);
    layout2->addWidget(splitter);
    widgets.append(qMakePair(widget2, QStringLiteral("tab1")));

    if(_AutoTempList.size() > 0)
    {
        TableWidgetForm* _TableWidgetForm = new TableWidgetForm(widget2);
        splitter->addWidget(_TableWidgetForm);
        _TableWidgetForm->setGroupBoxTitle(QString("%1_1").arg(fileName.left(fileName.size()-4)));

        //获取表头
        AutoTemp _AutoTemptemp;
        _TableWidgetForm->setTableWidgetHeaderLabels(COMMONS->getTableWidgetHeadLabels(fileName,_AutoTemptemp.toFieldNames().split(',')));
        _TableWidgetForm->setTableWidgetHeaderType(_AutoTemptemp.toFieldTypes());

        for (int j = 0; j < _AutoTempList.size(); j++)
        {
            _TableWidgetForm->getTableWidget()->setRowCount(j+1);
            _TableWidgetForm->getTableWidget()->setItem(j, 0, new QTableWidgetItem(_AutoTempList.at(j).name));
            _TableWidgetForm->getTableWidget()->item(j, 0)->setData(Qt::UserRole, _AutoTempList.at(j).id);
            _TableWidgetForm->getTableWidget()->setItem(j, 1, new QTableWidgetItem(QString("%1").arg(_AutoTempList.at(j).val)));
            _TableWidgetForm->getTableWidget()->setItem(j, 2, new QTableWidgetItem(QString("%1").arg(_AutoTempList.at(j).AirInletTempDevi)));
        }
        QList<TableWidgetForm*> item = m_pTableWidgetFormMap.value(1);
        m_pTableWidgetFormMap.insert(1, item << _TableWidgetForm);
    }

    if(_AutoAreaList.size() > 0)
    {
        TableWidgetForm* _TableWidgetForm = new TableWidgetForm(widget2);
        splitter->addWidget(_TableWidgetForm);
        _TableWidgetForm->setGroupBoxTitle(QString("%1_2").arg(fileName.left(fileName.size()-4)));

        //获取表头
        AutoArea _AutoAreatemp;
        _TableWidgetForm->setTableWidgetHeaderLabels(COMMONS->getTableWidgetHeadLabels(fileName,_AutoAreatemp.toFieldNames().split(',')));
        _TableWidgetForm->setTableWidgetHeaderType(_AutoAreatemp.toFieldTypes());

        for (int j = 0; j < _AutoAreaList.size(); j++)
        {
            _TableWidgetForm->getTableWidget()->setRowCount(j+1);
            _TableWidgetForm->getTableWidget()->setItem(j, 0, new QTableWidgetItem(_AutoAreaList.at(j).name));
            _TableWidgetForm->getTableWidget()->item(j, 0)->setData(Qt::UserRole, _AutoAreaList.at(j).id);
            _TableWidgetForm->getTableWidget()->setItem(j, 1, new QTableWidgetItem(QString("%1").arg(_AutoAreaList.at(j).in_sensor)));
            _TableWidgetForm->getTableWidget()->setItem(j, 2, new QTableWidgetItem(QString("%1").arg(_AutoAreaList.at(j).out_sensor)));
            _TableWidgetForm->getTableWidget()->setItem(j, 3, new QTableWidgetItem(QString("%1").arg(_AutoAreaList.at(j).water_in_sensor)));
            _TableWidgetForm->getTableWidget()->setItem(j, 4, new QTableWidgetItem(QString("%1").arg(_AutoAreaList.at(j).water_out_sensor)));
        }
        QList<TableWidgetForm*> item = m_pTableWidgetFormMap.value(1);
        m_pTableWidgetFormMap.insert(1, item << _TableWidgetForm);
    }

    if(_DevModeItemList.size() > 0)
    {
        TableWidgetForm* _TableWidgetForm = new TableWidgetForm(widget2);
        splitter->addWidget(_TableWidgetForm);
        _TableWidgetForm->setGroupBoxTitle(QString("%1_3").arg(fileName.left(fileName.size()-4)));

        //获取表头
        DevModeItem _DevModeItemtemp;
        _TableWidgetForm->setTableWidgetHeaderLabels(COMMONS->getTableWidgetHeadLabels(fileName,_DevModeItemtemp.toFieldNames().split(',')));
        _TableWidgetForm->setTableWidgetHeaderType(_DevModeItemtemp.toFieldTypes());

        for (int j = 0; j < _DevModeItemList.size(); j++)
        {
            _TableWidgetForm->getTableWidget()->setRowCount(j+1);
            _TableWidgetForm->getTableWidget()->setItem(j, 0, new QTableWidgetItem(_DevModeItemList.at(j).name));
            _TableWidgetForm->getTableWidget()->item(j, 0)->setData(Qt::UserRole, _DevModeItemList.at(j).id);
            _TableWidgetForm->getTableWidget()->setItem(j, 1, new QTableWidgetItem(QString("%1").arg(_DevModeItemList.at(j).mode)));
        }
        QList<TableWidgetForm*> item = m_pTableWidgetFormMap.value(1);
        m_pTableWidgetFormMap.insert(1, item << _TableWidgetForm);
    }

    for (int i=0;i<_AreasList.size();i++)
    {
        TableWidgetForm* _TableWidgetForm = new TableWidgetForm(widget2);
        splitter->addWidget(_TableWidgetForm);
        _TableWidgetForm->setGroupBoxTitle(QString("%1_%2").arg(fileName.left(fileName.size()-4)).arg(i*2+4));

        //获取表头
        AreasArea _AreasAreatemp;
        _TableWidgetForm->setTableWidgetHeaderLabels(COMMONS->getTableWidgetHeadLabels(fileName,_AreasAreatemp.toFieldNames().split(',')));
        _TableWidgetForm->setTableWidgetHeaderType(_AreasAreatemp.toFieldTypes());

        _TableWidgetForm->getTableWidget()->setRowCount(1);
        _TableWidgetForm->getTableWidget()->setItem(0, 0, new QTableWidgetItem(_AreasList.at(i).first.name));
        _TableWidgetForm->getTableWidget()->item(0, 0)->setData(Qt::UserRole, _AreasList.at(i).first.id);
        _TableWidgetForm->getTableWidget()->setItem(0, 1, new QTableWidgetItem(QString("%1").arg(_AreasList.at(i).first.mode)));
        _TableWidgetForm->getTableWidget()->setItem(0, 2, new QTableWidgetItem(QString("%1").arg(_AreasList.at(i).first.gear)));

        TableWidgetForm* _TableWidgetForm2 = new TableWidgetForm(widget2);
        splitter->addWidget(_TableWidgetForm2);
        _TableWidgetForm2->setGroupBoxTitle(QString("%1_%2").arg(fileName.left(fileName.size()-4)).arg(i*2+1+4));

        //获取表头
        AreaGear _AreaGeartemp;
        _TableWidgetForm2->setTableWidgetHeaderLabels(COMMONS->getTableWidgetHeadLabels(fileName,_AreaGeartemp.toFieldNames().split(',')));

        for (int j = 0; j < _AreasList.at(i).second.size(); j++)
        {
            _TableWidgetForm2->getTableWidget()->setRowCount(j+1);
            _TableWidgetForm2->getTableWidget()->setItem(j, 0, new QTableWidgetItem(QString("%1").arg(_AreasList.at(i).second.at(j).rotate)));
            _TableWidgetForm2->getTableWidget()->item(j, 0)->setData(Qt::UserRole, _AreasList.at(i).second.at(j).id);
            _TableWidgetForm2->getTableWidget()->setItem(j, 1, new QTableWidgetItem(QString("%1").arg(_AreasList.at(i).second.at(j).angle)));
        }

        QList<TableWidgetForm*> item = m_pTableWidgetFormMap.value(1);
        m_pTableWidgetFormMap.insert(1, item << _TableWidgetForm << _TableWidgetForm2);
    }
}

bool AutoControl1028::SaveUI(const QString &fileName, const QMap<int, QList<TableWidgetForm *> > &m_pTableWidgetFormMap)
{
    QString filePath = QString("%1/config/%2/%3")
            .arg(COMMONS->XmlPath)
            .arg(COMMONS->MachType)
            .arg(fileName);
    if(m_pTableWidgetFormMap.count() > 0)
    {
        QList<AutoItem> _AutoItemList;
        QList<AutoTemp> _AutoTempList;
        QList<AutoArea> _AutoAreaList;
        QList<DevModeItem> _DevModeItemList;
        QList<QPair<AreasArea, QList<AreaGear>>> _AreasList;

        TableWidgetForm* _TableWidgetForm = m_pTableWidgetFormMap.value(0).at(0);
        if(_TableWidgetForm)
        {
            for(int k = 0;k<_TableWidgetForm->getTableWidget()->rowCount();k++)
            {
                AutoItem _AutoItem;
                _AutoItem.id = _TableWidgetForm->getTableWidget()->item(k,0)->data(Qt::UserRole).toInt();
                _AutoItem.name = _TableWidgetForm->getTableWidget()->item(k,0)->text();
                _AutoItem.val = _TableWidgetForm->getTableWidget()->item(k,1)->text().toInt();
                _AutoItem.desc = _TableWidgetForm->getTableWidget()->item(k,2)->text();
                _AutoItemList.append(_AutoItem);
            }
        }

        if(m_pTableWidgetFormMap.value(1).size() > 0)
        {
            TableWidgetForm* _TableWidgetForm = m_pTableWidgetFormMap.value(1).at(0);
            if(_TableWidgetForm)
            {
                for(int k = 0;k<_TableWidgetForm->getTableWidget()->rowCount();k++)
                {
                    AutoTemp _AutoTemp;
                    _AutoTemp.id = _TableWidgetForm->getTableWidget()->item(k,0)->data(Qt::UserRole).toInt();
                    _AutoTemp.name = _TableWidgetForm->getTableWidget()->item(k,0)->text();
                    _AutoTemp.val = _TableWidgetForm->getTableWidget()->item(k,1)->text().toInt();
                    _AutoTemp.AirInletTempDevi = _TableWidgetForm->getTableWidget()->item(k,2)->text().toInt();
                    _AutoTempList.append(_AutoTemp);
                }
            }
        }
        if(m_pTableWidgetFormMap.value(1).size() > 1)
        {
            TableWidgetForm* _TableWidgetForm = m_pTableWidgetFormMap.value(1).at(1);
            if(_TableWidgetForm)
            {
                for(int k = 0;k<_TableWidgetForm->getTableWidget()->rowCount();k++)
                {
                    AutoArea _AutoArea;
                    _AutoArea.id = _TableWidgetForm->getTableWidget()->item(k,0)->data(Qt::UserRole).toInt();
                    _AutoArea.name = _TableWidgetForm->getTableWidget()->item(k,0)->text();
                    _AutoArea.in_sensor = _TableWidgetForm->getTableWidget()->item(k,1)->text();
                    _AutoArea.out_sensor = _TableWidgetForm->getTableWidget()->item(k,2)->text();
                    _AutoArea.water_in_sensor = _TableWidgetForm->getTableWidget()->item(k,3)->text().toInt();
                    _AutoArea.water_out_sensor = _TableWidgetForm->getTableWidget()->item(k,4)->text().toInt();
                    _AutoAreaList.append(_AutoArea);
                }
            }
        }

        if(m_pTableWidgetFormMap.value(1).size() > 2)
        {
            TableWidgetForm* _TableWidgetForm = m_pTableWidgetFormMap.value(1).at(2);
            if(_TableWidgetForm)
            {
                for(int k = 0;k<_TableWidgetForm->getTableWidget()->rowCount();k++)
                {
                    DevModeItem _DevModeItem;
                    _DevModeItem.id = _TableWidgetForm->getTableWidget()->item(k,0)->data(Qt::UserRole).toInt();
                    _DevModeItem.name = _TableWidgetForm->getTableWidget()->item(k,0)->text();
                    _DevModeItem.mode = _TableWidgetForm->getTableWidget()->item(k,1)->text().toInt();
                    _DevModeItemList.append(_DevModeItem);
                }
            }
        }

        for (int i = 3;i<m_pTableWidgetFormMap.value(1).size();i+=2)
        {
            QPair<AreasArea, QList<AreaGear>> AreasAreaPair;

            TableWidgetForm* _TableWidgetForm = m_pTableWidgetFormMap.value(1).at(i);
            if(_TableWidgetForm)
            {
                AreasAreaPair.first.id = _TableWidgetForm->getTableWidget()->item(0,0)->data(Qt::UserRole).toInt();
                AreasAreaPair.first.name = _TableWidgetForm->getTableWidget()->item(0,0)->text();
                AreasAreaPair.first.mode = _TableWidgetForm->getTableWidget()->item(0,1)->text().toInt();
                AreasAreaPair.first.gear = _TableWidgetForm->getTableWidget()->item(0,2)->text().toInt();
            }

            TableWidgetForm* _TableWidgetForm2 = m_pTableWidgetFormMap.value(1).at(i+1);
            if(_TableWidgetForm2)
            {
                for(int k = 0;k<_TableWidgetForm2->getTableWidget()->rowCount();k++)
                {
                    AreaGear _AreaGear;
                    _AreaGear.id = _TableWidgetForm2->getTableWidget()->item(k,0)->data(Qt::UserRole).toInt();
                    _AreaGear.rotate = _TableWidgetForm2->getTableWidget()->item(k,0)->text().toInt();
                    _AreaGear.angle = _TableWidgetForm2->getTableWidget()->item(k,1)->text().toInt();
                    AreasAreaPair.second.append(_AreaGear);
                }
            }
            _AreasList.append(AreasAreaPair);
        }

        SaveXml(filePath,_AutoItemList,_AutoTempList,_AutoAreaList,_DevModeItemList,_AreasList);
        return true;
    }
    return false;
}

void AutoControl1028::Analysis(const QString &path, QList<AutoItem> &_AutoItemList, QList<AutoTemp> &_AutoTempList, QList<AutoArea> &_AutoAreaList, QList<DevModeItem> &_DevModeItemList, QList<QPair<AreasArea, QList<AreaGear> > > &_AreasList)
{
    TiXmlDocument* xmlDocument = new TiXmlDocument();
    if(!xmlDocument->LoadFile(path.toStdString().c_str()))
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
        return;
    }

    TiXmlElement* rootElement = xmlDocument->FirstChildElement("AutoControl");
    if(rootElement == nullptr) //节点不存在
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
        return;
    }

    if(rootElement)
    {
        TiXmlElement* AutoAdjustParamsElement = rootElement->FirstChildElement("AutoAdjustParams");
        if(AutoAdjustParamsElement != nullptr)
        {
            for(TiXmlElement* ItemElement = AutoAdjustParamsElement->FirstChildElement("Item");
                ItemElement != nullptr; ItemElement = ItemElement->NextSiblingElement("Item"))
            {
                AutoItem _AutoItem;
                ItemElement->QueryIntAttribute("id", &_AutoItem.id);
                _AutoItem.name = ItemElement->Attribute("name");
                ItemElement->QueryIntAttribute("val", &_AutoItem.val);
                _AutoItem.desc = ItemElement->Attribute("desc");
                _AutoItemList.append(_AutoItem);
            }
        }

        TiXmlElement* AutoTempSetElement = rootElement->FirstChildElement("AutoTempSet");
        if(AutoTempSetElement != nullptr)
        {
            for(TiXmlElement* TempElement = AutoTempSetElement->FirstChildElement("Temp");
                TempElement != nullptr; TempElement = TempElement->NextSiblingElement("Temp"))
            {
                AutoTemp _AutoTemp;
                TempElement->QueryIntAttribute("id", &_AutoTemp.id);
                _AutoTemp.name = TempElement->Attribute("name");
                TempElement->QueryIntAttribute("val", &_AutoTemp.val);
                TempElement->QueryIntAttribute("AirInletTempDevi", &_AutoTemp.AirInletTempDevi);
                _AutoTempList.append(_AutoTemp);
            }
        }

        TiXmlElement* TempSensorElement = rootElement->FirstChildElement("TempSensor");
        if(TempSensorElement != nullptr)
        {
            for(TiXmlElement* AreaElement = TempSensorElement->FirstChildElement("Area");
                AreaElement != nullptr; AreaElement = AreaElement->NextSiblingElement("Area"))
            {
                AutoArea _AutoArea;
                AreaElement->QueryIntAttribute("id", &_AutoArea.id);
                _AutoArea.name = AreaElement->Attribute("name");
                _AutoArea.in_sensor = AreaElement->Attribute("in_sensor");
                _AutoArea.out_sensor = AreaElement->Attribute("out_sensor");
                AreaElement->QueryIntAttribute("water_in_sensor", &_AutoArea.water_in_sensor);
                AreaElement->QueryIntAttribute("water_out_sensor", &_AutoArea.water_out_sensor);
                _AutoAreaList.append(_AutoArea);
            }
        }

        TiXmlElement* DevModeElement = rootElement->FirstChildElement("DevMode");
        if(DevModeElement != nullptr)
        {
            for(TiXmlElement* ItemElement = DevModeElement->FirstChildElement("Item");
                ItemElement != nullptr; ItemElement = ItemElement->NextSiblingElement("Item"))
            {
                DevModeItem _DevModeItem;
                ItemElement->QueryIntAttribute("id", &_DevModeItem.id);
                _DevModeItem.name = ItemElement->Attribute("name");
                ItemElement->QueryIntAttribute("mode", &_DevModeItem.mode);
                _DevModeItemList.append(_DevModeItem);
            }
        }

        TiXmlElement* AreasElement = rootElement->FirstChildElement("Areas");
        if(AreasElement != nullptr)
        {
            for(TiXmlElement* AreaElement = AreasElement->FirstChildElement("Area");
                AreaElement != nullptr; AreaElement = AreaElement->NextSiblingElement("Area"))
            {
                QPair<AreasArea, QList<AreaGear>> AreasPair;
                AreaElement->QueryIntAttribute("id", &AreasPair.first.id);
                AreasPair.first.name = AreaElement->Attribute("name");
                AreaElement->QueryIntAttribute("mode", &AreasPair.first.mode);
                AreaElement->QueryIntAttribute("gear", &AreasPair.first.gear);

                for(TiXmlElement* GearElement = AreaElement->FirstChildElement("Gear");
                    GearElement != nullptr; GearElement = GearElement->NextSiblingElement("Gear"))
                {
                    AreaGear _AreaGear;
                    GearElement->QueryIntAttribute("id", &_AreaGear.id);
                    GearElement->QueryIntAttribute("rotate", &_AreaGear.rotate);
                    GearElement->QueryIntAttribute("angle", &_AreaGear.angle);
                    AreasPair.second.append(_AreaGear);
                }
                _AreasList.append(AreasPair);
            }
        }
    }
    delete xmlDocument;
}

bool AutoControl1028::SaveXml(const QString &path, const QList<AutoItem> &_AutoItemList, const QList<AutoTemp> &_AutoTempList, const QList<AutoArea> &_AutoAreaList, const QList<DevModeItem> &_DevModeItemList, const QList<QPair<AreasArea, QList<AreaGear> > > &_AreasList)
{
    TiXmlDocument* xmlDocument = new TiXmlDocument();
    if(!xmlDocument->LoadFile(path.toStdString().c_str()))
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
    }

    TiXmlElement* rootElement = xmlDocument->FirstChildElement("AutoControl");
    if(rootElement == nullptr) //节点不存在
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
    }

    if(rootElement)
    {
        TiXmlElement* AutoAdjustParamsElement = rootElement->FirstChildElement("AutoAdjustParams");
        if(AutoAdjustParamsElement != nullptr)
        {
            for(TiXmlElement* ItemElement = AutoAdjustParamsElement->FirstChildElement("Item");
                ItemElement != nullptr; ItemElement = ItemElement->NextSiblingElement("Item"))
            {
                AutoAdjustParamsElement->RemoveChild(ItemElement);
            }
            for (auto _AutoItem : _AutoItemList)
            {
                //元素类
                TiXmlElement* ChildxmlElement = new TiXmlElement("Item");
                ChildxmlElement->SetAttribute("id", _AutoItem.id);
                ChildxmlElement->SetAttribute("name", _AutoItem.name.toStdString().c_str());
                ChildxmlElement->SetAttribute("val", _AutoItem.val);
                ChildxmlElement->SetAttribute("desc", _AutoItem.desc.toStdString().c_str());
                AutoAdjustParamsElement->LinkEndChild(ChildxmlElement);
            }
        }

        TiXmlElement* AutoTempSetElement = rootElement->FirstChildElement("AutoTempSet");
        if(AutoTempSetElement != nullptr)
        {
            for(TiXmlElement* TempElement = AutoTempSetElement->FirstChildElement("Temp");
                TempElement != nullptr; TempElement = TempElement->NextSiblingElement("Temp"))
            {
                AutoTempSetElement->RemoveChild(TempElement);
            }
            for (auto _AutoTemp : _AutoTempList)
            {
                //元素类
                TiXmlElement* ChildxmlElement = new TiXmlElement("Temp");
                ChildxmlElement->SetAttribute("id", _AutoTemp.id);
                ChildxmlElement->SetAttribute("name", _AutoTemp.name.toStdString().c_str());
                ChildxmlElement->SetAttribute("val", _AutoTemp.val);
                ChildxmlElement->SetAttribute("AirInletTempDevi", _AutoTemp.AirInletTempDevi);
                AutoTempSetElement->LinkEndChild(ChildxmlElement);
            }
        }

        TiXmlElement* TempSensorElement = rootElement->FirstChildElement("TempSensor");
        if(TempSensorElement != nullptr)
        {
            for(TiXmlElement* AreaElement = TempSensorElement->FirstChildElement("Area");
                AreaElement != nullptr; AreaElement = AreaElement->NextSiblingElement("Area"))
            {
                TempSensorElement->RemoveChild(AreaElement);
            }
            for (auto _AutoArea : _AutoAreaList)
            {
                //元素类
                TiXmlElement* ChildxmlElement = new TiXmlElement("Area");
                ChildxmlElement->SetAttribute("id", _AutoArea.id);
                ChildxmlElement->SetAttribute("name", _AutoArea.name.toStdString().c_str());
                ChildxmlElement->SetAttribute("in_sensor", _AutoArea.in_sensor.toStdString().c_str());
                ChildxmlElement->SetAttribute("out_sensor", _AutoArea.out_sensor.toStdString().c_str());
                ChildxmlElement->SetAttribute("water_in_sensor", _AutoArea.water_in_sensor);
                ChildxmlElement->SetAttribute("water_out_sensor", _AutoArea.water_out_sensor);
                TempSensorElement->LinkEndChild(ChildxmlElement);
            }
        }

        TiXmlElement* DevModeElement = rootElement->FirstChildElement("DevMode");
        if(DevModeElement != nullptr)
        {
            for(TiXmlElement* ItemElement = DevModeElement->FirstChildElement("Item");
                ItemElement != nullptr; ItemElement = ItemElement->NextSiblingElement("Item"))
            {
                DevModeElement->RemoveChild(ItemElement);
            }
            for (auto _DevModeItem : _DevModeItemList)
            {
                //元素类
                TiXmlElement* ChildxmlElement = new TiXmlElement("Item");
                ChildxmlElement->SetAttribute("id", _DevModeItem.id);
                ChildxmlElement->SetAttribute("name", _DevModeItem.name.toStdString().c_str());
                ChildxmlElement->SetAttribute("mode", _DevModeItem.mode);
                DevModeElement->LinkEndChild(ChildxmlElement);
            }
        }

        int index = 0;
        TiXmlElement* AreasElement = rootElement->FirstChildElement("Areas");
        if(AreasElement != nullptr)
        {
            for(TiXmlElement* AreaElement = AreasElement->FirstChildElement("Area");
                AreaElement != nullptr; AreaElement = AreaElement->NextSiblingElement("Area"))
            {
                AreaElement->SetAttribute("id", _AreasList.at(index).first.id);
                AreaElement->SetAttribute("name", _AreasList.at(index).first.name.toStdString().c_str());
                AreaElement->SetAttribute("mode", _AreasList.at(index).first.mode);
                AreaElement->SetAttribute("gear", _AreasList.at(index).first.gear);

                for(TiXmlElement* GearElement = AreaElement->FirstChildElement("Gear");
                    GearElement != nullptr; GearElement = GearElement->NextSiblingElement("Gear"))
                {
                    AreaElement->RemoveChild(GearElement);
                }

                for (auto item : _AreasList.at(index).second)
                {
                    //元素类
                    TiXmlElement* ChildxmlElement = new TiXmlElement("Gear");
                    ChildxmlElement->SetAttribute("id", item.id);
                    ChildxmlElement->SetAttribute("rotate", item.rotate);
                    ChildxmlElement->SetAttribute("angle", item.angle);
                    AreaElement->LinkEndChild(ChildxmlElement);
                }
                index++;
            }
        }
    }
    //保存
    xmlDocument->SaveFile(path.toStdString().c_str());

    delete xmlDocument;
    return true;
}
